/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Impressao;

import java.net.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.OrientationRequested;
import javax.swing.RepaintManager;




public class Print2DPrinterJob implements Printable {
private String text;
 private Component componentToBePrinted;
/**
* Efetua impressão do texto passado como parâmetro.
* @param texto
*/
public Print2DPrinterJob(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
aset.add(new Copies(1));
aset.add(new JobName("Relatório", null));
/*
* Crai um "Printer job"
*/
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(this);

/*
* Localiza um serviço de impressão
* que possa tratar esta requisção.
*/
PrintService[] services = PrinterJob.lookupPrintServices();

if (services.length > 0) {
System.out.println("Impressora seleionada" + services[0].getName());
try {
pj.setPrintService(services[0]);
pj.pageDialog(aset);
if (pj.printDialog(aset)) {
pj.print(aset);
}
} catch (PrinterException pe) {
System.err.println(pe);
}
}
}

/**
* Método necessário quando a interface Printable é implementada.
*/
public int print(Graphics g, PageFormat pf, int pageIndex) {
    int response = NO_SUCH_PAGE;

    Graphics2D g2 = (Graphics2D) g;

    //  for faster printing, turn off double buffering
    disableDoubleBuffering(componentToBePrinted);

    Dimension d = componentToBePrinted.getSize(); //get size of document
    double panelWidth = d.width ; //width in pixels (I ADDED THE + 144 to fix the right margin)
    double panelHeight = d.height; //height in pixels

    double pageHeight = pf.getImageableHeight(); //height of printer page
    double pageWidth = pf.getImageableWidth(); //width of printer page

    double scale = pageWidth / panelWidth;
    int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);

    //  make sure not print empty pages
    if (pageIndex >= totalNumPages) {
      response = NO_SUCH_PAGE;
    }
    else {

      //  shift Graphic to line up with beginning of print-imageable region
      g2.translate(pf.getImageableX() , pf.getImageableY() ); //(I ADDED THE + 72 to fix the left and top margins)

      //  shift Graphic to line up with beginning of next page to print
      g2.translate(0f, pageIndex * pageHeight);

      //  scale the page so the width fits...
      g2.scale(scale, scale);

      componentToBePrinted.paint(g2); //repaint the page for printing

      enableDoubleBuffering(componentToBePrinted);
      response = Printable.PAGE_EXISTS;
    }
    return response;
  }

  public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }

  public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }
}